package com.xtgg.login;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.umeng.message.UmengRegistrar;
import com.xtgg.baby.BabyListActivity;
import com.xtgg.baby.DeviceSearchActivity;
import com.xtgg.main.MainActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.model.Resp;
import com.xtgg.R;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.PreferencesUtils;
import com.xtgg.util.TLog;
import com.xtgg.util.ToastUtil;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends BaseActivity implements OnClickListener {
	EditText etTel;
	EditText etPwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		etTel = (EditText) findViewById(R.id.etTel);
		etPwd = (EditText) findViewById(R.id.etPwd);

		((TextView) findViewById(R.id.center)).setText("登录");
		((Button) findViewById(R.id.right)).setText("   注册   ");
		((Button) findViewById(R.id.right))
				.setBackgroundResource(R.drawable.corner_orange_bord);
		((Button) findViewById(R.id.right)).setTextColor(Color
				.parseColor("#EE7C1C"));

		findViewById(R.id.login).setOnClickListener(this);
		findViewById(R.id.forget).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.left).setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		int viewId = arg0.getId();
		switch (viewId) {
		case R.id.login:
			sendHttpRequest();
			break;

		case R.id.forget:
			forgetPwd();
			break;

		case R.id.left:
			finish();
			break;

		case R.id.right: {
			Intent intent = new Intent(this, TelCodeActivity.class);
			intent.putExtra("type", "regist");
			startActivity(intent);

			finish();
		}
			break;
		}
	}

	void forgetPwd() {
		Intent intent = new Intent(this, TelCodeActivity.class);
		intent.putExtra("type", "forget");
		intent.putExtra("tel", etTel.getText().toString());
		startActivity(intent);

		finish();
	}

	private void sendHttpRequest() {
		if (etTel.getText().toString().length() == 0
				|| etPwd.getText().toString().length() == 0) {
			ToastUtil.showToastWithAlertPic("请输入手机号码或密码");
			return;
		}

		((Button)findViewById(R.id.login)).setSelected(true);
		((Button)findViewById(R.id.login)).setOnClickListener(null);
		
		MgqDataHandler loginHandler = new MgqDataHandler(LoginActivity.this,
				true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);

				if (ruser.getState().equals("1")) {
					JSONObject obj = JSON.parseObject(response)
							.getJSONArray("data").getJSONObject(0);

					PreferencesUtils.putString(LoginActivity.this, "password",
							etPwd.getText().toString());
					PreferencesUtils.putString(LoginActivity.this, "address",
							obj.getString("address"));
					PreferencesUtils.putString(LoginActivity.this, "headurl",
							obj.getString("headurl"));
					PreferencesUtils.putString(LoginActivity.this, "nickname",
							obj.getString("nickname"));
					PreferencesUtils.putString(LoginActivity.this, "telephone",
							obj.getString("telephone"));
					PreferencesUtils.putString(LoginActivity.this, "userid",
							obj.getString("userid"));
					PreferencesUtils.putString(LoginActivity.this, "username",
							obj.getString("username"));
					PreferencesUtils.putString(LoginActivity.this, "sex",
							obj.getString("sex"));

					sendCheckHttpRequest();
				} else {
					((Button)findViewById(R.id.login)).setSelected(false);
					((Button)findViewById(R.id.login)).setOnClickListener(LoginActivity.this);
					
					ToastUtil.showToastWithAlertPic(ruser.getMessage());
				}
			}
		};
		// -----------------------

		RequestParams params = new RequestParams();
		params.put("act", "userlogin");
		params.put("mobile", etTel.getText().toString());
		params.put("password", etPwd.getText().toString());
		params.put("device_token", UmengRegistrar.getRegistrationId(this));
		if(((DemoApp)getApplicationContext()).mLng > 1 && Math.abs(((DemoApp)getApplicationContext()).mLng) < 180)
		{
			params.put("longitude", ((DemoApp)getApplicationContext()).mLng);
			params.put("latitude", ((DemoApp)getApplicationContext()).mLat);
		}
		MgqRestClient.get(Constants.USERMGT, params, loginHandler);
	}

	private void sendCheckHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(LoginActivity.this,
				true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);
				List<Activity> list = ((DemoApp)getApplicationContext()).getActivityList();
				int size = list.size();
				
				if(ruser.getState().equals("1"))
				{
					startActivity(new Intent(LoginActivity.this, MainActivity.class));
				}
				else
				{
					BluetoothAdapter.getDefaultAdapter().enable();
					startActivity(new Intent(LoginActivity.this,DeviceSearchActivity.class));
				}
				
				for(int i = 0; i < size; i++)
				{
					list.get(i).finish();
				}
				finish();
			}
		};
		// -----------------------

		RequestParams params = new RequestParams();
		params.put("act", "getMyDeviceList");
		params.put("userid", PreferencesUtils.getString(this, "userid"));
		MgqRestClient.get(Constants.DEVICEMGT, params, loginHandler);
	}
}
